package StudyCode.summary.mycollection;

import java.util.*;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class UnionOfListOfLists {

	public static <T> Set<T> unionOfListOfLists(
			final Collection<? extends Collection<T>> collectionOfCollection) {
		if (collectionOfCollection == null
				|| collectionOfCollection.isEmpty()) {
			return new HashSet<T>(0);
		}
		final HashSet<T> union = new HashSet<T>();
		for (final Collection<T> col : collectionOfCollection) {
			if (col != null) {
				for (final T t : col) {
					if (t != null) {
						union.add(t);
					}
				}
			}
		}
		return union;
	}

	public static <T> List<T> asList(final Iterable<? extends T> iterable) {
		return (iterable instanceof Collection) ? new LinkedList<T>(
				(Collection<? extends T>) iterable) : new LinkedList<T>() {
			private static final long serialVersionUID = 3109256773218160485L;

			{
				if (iterable != null) {
					for (final T t : iterable) {
						add(t);
					}
				}
			}
		};
	}

	public static <T> List<T> asList(final T t, final T... ts) {
		final ArrayList<T> list = new ArrayList<T>(ts.length + 1);
		list.add(t);
		Collections.addAll(list, ts);
		return list;
	}

	public static <T> boolean isEmpty(final Collection<T> collection) {
		return collection != null && collection.isEmpty();
	}

	public static <K, V> boolean isEmpty(final Map<K, V> map) {
		return map != null && map.isEmpty();
	}
}
